//
//  FoodCell.m
//  LoveTourism
//
//  Created by qianfeng0 on 16/6/24.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "FoodCell.h"
#import <UIImageView+WebCache.h>
#import "tagView.h"
#import "NSString+AutoHeight.h"
#import "Helper.h"
#import <Masonry.h>
@interface FoodCell ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag5Left;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag5Top;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag4Top;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag4Left;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewWidth;
@property (weak, nonatomic) IBOutlet tagView *tag4View;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag2ViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag3ViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag4ViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag5ViewWidth;
@property (weak, nonatomic) IBOutlet UIButton *opBtn;

@property (weak, nonatomic) IBOutlet UIImageView *foodImageView;

@property (weak, nonatomic) IBOutlet UILabel *title;

@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *tag1Lbael;
@property (weak, nonatomic) IBOutlet UILabel *tag2Label;
@property (weak, nonatomic) IBOutlet UILabel *tag3Label;
@property (weak, nonatomic) IBOutlet UILabel *tag4Lable;
@property (weak, nonatomic) IBOutlet UILabel *tag5Lable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tag1ViewWidth;
@property (weak, nonatomic) IBOutlet tagView *tag5View;
@end

@implementation FoodCell

- (IBAction)favoriteBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(btnClick:and:)]) {
        [self.delegate btnClick:sender and:self.indexPath];
    }
}

- (void)setModel:(FoodModel *)model {
    NSLog(@"%@",NSHomeDirectory());
    self.imageViewWidth.constant = ScreenWidth * 0.3;
    if ([self.flag isEqualToString:@"1"]) {
        [self.opBtn removeFromSuperview];
    } else {
        if (!self.opBtn.superview) {
            [self.contentView addSubview:self.opBtn];
        }
    }
    [_foodImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.title.text = model.title;
    self.desLabel.text = model.descriptions;
    self.contentLabel.text = model.content;
    self.tag1Lbael.text = [model.tagsInfo[0] text];
    self.tag1ViewWidth.constant = [[model.tagsInfo[0] text] ch_sizeWithFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT, 17)].width+15;
    self.tag2Label.text = [model.tagsInfo[1] text];
    self.tag2ViewWidth.constant = [[model.tagsInfo[1] text] ch_sizeWithFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT, 17)].width+15;
    self.tag3Label.text = [model.tagsInfo[2] text];
    self.tag3ViewWidth.constant = [[model.tagsInfo[2] text] ch_sizeWithFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT, 17)].width+15;
    self.tag4Lable.text = [model.tagsInfo[3] text];
    self.tag4ViewWidth.constant = [[model.tagsInfo[3] text] ch_sizeWithFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT, 17)].width+15;
    if (model.tagsInfo.count <= 4) {
        [self.tag5View removeFromSuperview];
    } else {
        if (self.tag5View.superview == nil) {
            [self.contentView addSubview:self.tag5View];
        }
        self.tag5Lable.text = [model.tagsInfo[4] text];
        self.tag5ViewWidth.constant = [[model.tagsInfo[4] text] ch_sizeWithFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT, 17)].width+15;
    }
    if (self.tag1ViewWidth.constant + self.tag2ViewWidth.constant + self.tag3ViewWidth.constant + self.tag4ViewWidth.constant >= ScreenWidth * 2/3 - 25) {
        [self.tag4View removeConstraints:@[self.tag4Left,self.tag4Top]];
        [self.tag4View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentLabel.mas_bottom).with.mas_offset(30);
            make.leading.mas_equalTo(self.tag5View.mas_trailing).with.mas_offset(8);
        }];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
